﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Bai1.Models;

namespace Bai1.Controllers
{
    public class ReviewsController : Controller
    {

        public ActionResult BestReview()
        {
            var bestReview = from r in _reviews
                         orderby r.Rating descending
                         select r;
            return PartialView("_Review",bestReview.First());
        }
        
        
        //
        // GET: /Reviews/

        public ActionResult Index()
        {
            var model = from s in _reviews
                        orderby s.Country
                        select s;
            return View(model);
        }

        //
        // GET: /Reviews/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Reviews/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Reviews/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Reviews/Edit/5

        public ActionResult Edit(int id)
        {
            var review = _reviews.Single(r => r.Id == id);
            return View(review);
        }

        //
        // POST: /Reviews/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                var review = _reviews.Single(r => r.Id == id);
                if (TryUpdateModel(review))
                {
                    return RedirectToAction("index");
                }

                return View(review);
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Reviews/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Reviews/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        static List<RestaurantReview> _reviews = new List<RestaurantReview>
        {
            new RestaurantReview{
                Id = 1,
                Name = "Cinnamon Club",
                City = "LonDon",
                Country= "UK",
                Rating = 12,
            },
             new RestaurantReview{
                Id = 2,
                Name = "Viet ban do",
                City = "HCM",
                Country= "Viet Nam",
                Rating = 102,
            },
             new RestaurantReview{
                Id = 3,
                Name = "Dong phuong",
                City = "Binh Dinh",
                Country= "Japan",
                Rating = 120,
            }, new RestaurantReview{
                Id = 4,
                Name = "Ngan hang",
                City = "<script> alert('Vung Tau')</script>",
                Country= "Trung Quoc",
                Rating = 45,
            }

        };

    }
}
